/**
 * Created by Administrator on 15-10-10.
 */
var address=[];

window.onload=function(){
    var url = getUrl();
    var id=getUserId();
    $.jsonp({
        url: 'http://'+url+'/api/cfg/common/country',
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            address = data;
            var html = "";
            $.each(address, function (index, province) {
                html += "<option value=\"" + province.code + "\">" + province.name + "</option><br/>";
            });
            $("#provinces").append(html);
            $("#provinces").on('change', function () {
                var code = $(this).val();
                var html = "";
                $.each(address, function (index, province) {
                    if (province.code == code) {
                        var citys = province.citys;
                        $.each(citys, function (index, citys) {
                            html += "<option value=\"" + citys.code + "\">" + citys.name + "</option><br/>";
                        });
                        $("#citys").append(html);
                    }
                });
            });

            $("#citys").on('change', function () {
                var province_code=$("#provinces").val();
                var code = $(this).val();
                var html = "";
                $.each(address, function (index, province) {
                    if (province.code == province_code) {
                        var citys = province.citys;
                        $.each(citys, function (index, citys) {
                            if (citys.code == code) {
                                var countys = citys.countys;
                                $.each(countys, function (index, countys) {
                                    html += "<option value=\"" + countys.code + "\">" + countys.name + "</option><br/>";
                                });
                                $("#countys").append(html);
                            }
                        });
                    }
                });
            });
            $(".countys").on('click',function(){
                var code=$(this).val();

            })
        },
        error: function (xOptions,textStatus) {
        }
    });


    $(".log_dizhi").on("click",function(){
        var realName=$("#realName").val();
        var telphone=$("#Telphone").val();
        var areaCode=$("#countys").val();
        var address=$("#Address").val();
        if(realName=="")
        {
            showToast("姓名不能为空~");
            return;
        }
        if(telphone=="")
        {
            showToast("手机号不能为空~");
            return;
        }
        if(telphone.length!=11)
        {
            showToast("手机号格式不对~");
            return;
        }
        if(areaCode=="")
        {
            showToast("所在地址不能为空~");
            return;
        }
        if(address=="")
        {
            showToast("详细地址不能为空~");
            return;
        }
        var userAddressId = $.getUrlParam("userAddressId");
        if(userAddressId){
            $.jsonp({
                url: 'http://'+url+'/api/user/update_address-'+userAddressId,
                data: {id:userAddressId,realName:realName,telphone:telphone,areaCode:areaCode,address:address,userId:id},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    if(data.succ==true){

                        //取得用户ID
                        location.href="dizhi.html";
                    }

                },
                error: function (xOptions,textStatus) {
                }
            });
        }else {
            $.jsonp({
                url: 'http://' + url + '/api/user/add_address',
                data: {realName: realName, telphone: telphone, areaCode: areaCode, address: address, userId: id},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    if (data.succ == true) {

                        //取得用户ID
                        location.href = "dizhi.html";
                    }

                },
                error: function (xOptions, textStatus) {
                }
            });
        }
    });
}